from OpenGL.GL import *
from OpenGL.GLU import *
import pyglet

from common.units import *
from game.core.gamemanager import GameManager

class MainScreen(object):
	def __init__(self, viewport):
		
		# Reference to the main window's window object.
		GameManager.camera.aspect = float(viewport[2]) / viewport[3] 
		self.viewport = viewport
	
	def mouse_press(self, x, y, button):
		clicked = GameManager.map.terrain.tryPick(GameManager.camera,
												x, y, self.viewport)
		if clicked is None: return
		
		if button == pyglet.window.mouse.LEFT:
			GameManager.selected = clicked.occupation
			GameManager.play_sound('walk_click')
					
		elif button == pyglet.window.mouse.RIGHT:
			GameManager.targeted = clicked
			if issubclass(GameManager.selected.__class__, AbstractUnit):
				if clicked.occupation is None:
					GameManager.play_sound('walk_click')
					GameManager.selected.move(clicked)
				elif issubclass(clicked.occupation.__class__, AbstractUnit):
					
					GameManager.selected.attack(clicked.occupation)
				
	
	def render(self):
		
		# Camera
		GameManager.camera.setLens()
		GameManager.camera.setView()
		
		GameManager.map.terrain.render()
			
		glDisable(GL_DEPTH_TEST)
		if not(GameManager.selected is None):
			GameManager.selected.render_selection()
		glEnable(GL_DEPTH_TEST)
			
#		if not(GameManager.targeted is None):
#			GameManager.targeted.render_selected()
		
		glColor(1, 1, 1)
		for unit in GameManager.map.units:
			unit.render()
			
		for building in GameManager.map.buildings:
			building.render()
			
		for doodad in GameManager.map.doodads:
			doodad.render()
			